package com.example;

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.entity.UserDetail;
import com.example.mapper.UserDetailMapper;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class NoSpring {
    private static SqlSessionFactory sqlSessionFactory = initSqlSessionFactory();
    public static void main(String[] args) {
        try (SqlSession session = sqlSessionFactory.openSession(true)) {
            UserDetailMapper mapper = session.getMapper(UserDetailMapper.class);
            System.out.println(mapper.selectList(Wrappers.lambdaQuery()));
            System.out.println(UserDetail.builder().build().selectAll());
        }
    }
    public static SqlSessionFactory initSqlSessionFactory() {
        DataSource dataSource = dataSource();
        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        Environment environment = new Environment("Production", transactionFactory, dataSource);
        MybatisConfiguration configuration = new MybatisConfiguration(environment);
        configuration.addMapper(UserDetailMapper.class);
        configuration.setLogImpl(StdOutImpl.class);
        configuration.setDefaultEnumTypeHandler(org.apache.ibatis.type.EnumOrdinalTypeHandler.class);
        return new MybatisSqlSessionFactoryBuilder().build(configuration);
    }
    public static DataSource dataSource() {
        SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
        dataSource.setDriverClass(com.mysql.cj.jdbc.Driver.class);
        dataSource.setUrl("jdbc:mysql://localhost:3306/scraper_db?autoReconnect=true&zeroDateTimeBehavior=CONVERT_TO_NULL&useUnicode=true&characterEncoding=utf-8&useSSL=false&nullCatalogMeansCurrent=true&serverTimezone=Asia/Shanghai&allowMultiQueries=true&allowPublicKeyRetrieval=true");
        dataSource.setUsername("wdc");
        dataSource.setPassword("wdc12345678");
        try (
                Connection connection = dataSource.getConnection();
                Statement statement = connection.createStatement();
                ResultSet resultSet = statement.executeQuery("SELECT VERSION()")) {
            // resultSet.next();
            // System.out.println(resultSet.getString("VERSION()"));
            // 在查询数据库版本时，使用列的索引（如resultSet.getString(1)）而不是列名（如resultSet.getString("VERSION()")），这样可以避免潜在的列名错误。
            if (resultSet.next()) {
                System.out.println(resultSet.getString(1)); // 使用索引获取第一个列的值
            } else {
                System.out.println("No results found.");
            }
        } catch (SQLException e) {
            System.out.println("Error connecting to database: " + e.getMessage());
            e.printStackTrace();
        }
        return dataSource;
    }
}